<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户ID" prop="customersId">
        <el-input
          v-model="queryParams.customersId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系ID" prop="contecterId">
        <el-input
          v-model="queryParams.contecterId"
          placeholder="请输入联系ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="ordersTime">
        <el-date-picker clearable
          v-model="queryParams.ordersTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发货姓名" prop="sendersName">
        <el-input
          v-model="queryParams.sendersName"
          placeholder="请输入发货姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货电话" prop="sendersPhone">
        <el-input
          v-model="queryParams.sendersPhone"
          placeholder="请输入发货电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货城市" prop="sendersAddress">
        <el-select v-model="queryParams.sendersAddress" placeholder="请选择发货城市" clearable>
          <el-option
            v-for="dict in dict.type.addresssss"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货地址" prop="sendersAddressDetail">
        <el-input
          v-model="queryParams.sendersAddressDetail"
          placeholder="请输入发货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货姓名" prop="receiversName">
        <el-input
          v-model="queryParams.receiversName"
          placeholder="请输入收货姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货电话" prop="receiversPhone">
        <el-input
          v-model="queryParams.receiversPhone"
          placeholder="请输入收货电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货城市" prop="receiversAddress">
        <el-select v-model="queryParams.receiversAddress" placeholder="请选择收货城市" clearable>
          <el-option
            v-for="dict in dict.type.addresssss"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收获地址" prop="receiversAddressDetail">
        <el-input
          v-model="queryParams.receiversAddressDetail"
          placeholder="请输入收获地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['demo2:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['demo2:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['demo2:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['demo2:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="ordersId" />
      <el-table-column label="客户ID" align="center" prop="customersId" />
      <el-table-column label="联系ID" align="center" prop="contecterId" />
      <el-table-column label="下单时间" align="center" prop="ordersTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ordersTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货姓名" align="center" prop="sendersName" />
      <el-table-column label="发货电话" align="center" prop="sendersPhone" />
      <el-table-column label="发货城市" align="center" prop="sendersAddress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.addresssss" :value="scope.row.sendersAddress"/>
        </template>
      </el-table-column>
      <el-table-column label="发货地址" align="center" prop="sendersAddressDetail" />
      <el-table-column label="收货姓名" align="center" prop="receiversName" />
      <el-table-column label="收货电话" align="center" prop="receiversPhone" />
      <el-table-column label="收货城市" align="center" prop="receiversAddress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.addresssss" :value="scope.row.receiversAddress"/>
        </template>
      </el-table-column>
      <el-table-column label="收获地址" align="center" prop="receiversAddressDetail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['demo2:orders:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['demo2:orders:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户ID" prop="customersId">
          <el-input v-model="form.customersId" placeholder="请输入客户ID" />
        </el-form-item>
        <el-form-item label="联系ID" prop="contecterId">
          <el-input v-model="form.contecterId" placeholder="请输入联系ID" />
        </el-form-item>
        <el-form-item label="下单时间" prop="ordersTime">
          <el-date-picker clearable
            v-model="form.ordersTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货姓名" prop="sendersName">
          <el-input v-model="form.sendersName" placeholder="请输入发货姓名" />
        </el-form-item>
        <el-form-item label="发货电话" prop="sendersPhone">
          <el-input v-model="form.sendersPhone" placeholder="请输入发货电话" />
        </el-form-item>
        <el-form-item label="发货城市" prop="sendersAddress">
          <el-select v-model="form.sendersAddress" placeholder="请选择发货城市">
            <el-option
              v-for="dict in dict.type.addresssss"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货地址" prop="sendersAddressDetail">
          <el-input v-model="form.sendersAddressDetail" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="收货姓名" prop="receiversName">
          <el-input v-model="form.receiversName" placeholder="请输入收货姓名" />
        </el-form-item>
        <el-form-item label="收货电话" prop="receiversPhone">
          <el-input v-model="form.receiversPhone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="收货城市" prop="receiversAddress">
          <el-select v-model="form.receiversAddress" placeholder="请选择收货城市">
            <el-option
              v-for="dict in dict.type.addresssss"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收获地址" prop="receiversAddressDetail">
          <el-input v-model="form.receiversAddressDetail" placeholder="请输入收获地址" />
        </el-form-item>
        <el-divider content-position="center">货物管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPackages">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePackages">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="packagesList" :row-class-name="rowPackagesIndex" @selection-change="handlePackagesSelectionChange" ref="packages">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="货物名称" prop="packagesName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.packagesName" placeholder="请输入货物名称" />
            </template>
          </el-table-column>
          <el-table-column label="货物类型" prop="packingtype" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.packingtype" placeholder="请选择货物类型">
                <el-option
                  v-for="dict in dict.type.packages_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="包装类型" prop="packingname" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.packingname" placeholder="请选择包装类型">
                <el-option
                  v-for="dict in dict.type.packing_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unitName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unitName" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="长" prop="length" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.length" placeholder="请输入长" />
            </template>
          </el-table-column>
          <el-table-column label="宽" prop="width" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.width" placeholder="请输入宽" />
            </template>
          </el-table-column>
          <el-table-column label="高" prop="height" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.height" placeholder="请输入高" />
            </template>
          </el-table-column>
          <el-table-column label="重量" prop="weight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" placeholder="请输入重量" />
            </template>
          </el-table-column>
          <el-table-column label="保鲜温度" prop="temperature" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.temperature" placeholder="请输入保鲜温度" />
            </template>
          </el-table-column>
          <el-table-column label="保质期" prop="shelfLife" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shelfLife" placeholder="请输入保质期" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/demo2/orders";

export default {
  name: "Orders",
  dicts: ['addresssss', 'packing_type', 'packages_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPackages: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      ordersList: [],
      // 货物管理表格数据
      packagesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customersId: null,
        contecterId: null,
        ordersTime: null,
        sendersName: null,
        sendersPhone: null,
        sendersAddress: null,
        sendersAddressDetail: null,
        receiversName: null,
        receiversPhone: null,
        receiversAddress: null,
        receiversAddressDetail: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customersId: [
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],
        contecterId: [
          { required: true, message: "联系ID不能为空", trigger: "blur" }
        ],
        ordersTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        sendersName: [
          { required: true, message: "发货姓名不能为空", trigger: "blur" }
        ],
        sendersPhone: [
          { required: true, message: "发货电话不能为空", trigger: "blur" }
        ],
        sendersAddress: [
          { required: true, message: "发货城市不能为空", trigger: "change" }
        ],
        sendersAddressDetail: [
          { required: true, message: "发货地址不能为空", trigger: "blur" }
        ],
        receiversName: [
          { required: true, message: "收货姓名不能为空", trigger: "blur" }
        ],
        receiversPhone: [
          { required: true, message: "收货电话不能为空", trigger: "blur" }
        ],
        receiversAddress: [
          { required: true, message: "收货城市不能为空", trigger: "change" }
        ],
        receiversAddressDetail: [
          { required: true, message: "收获地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ordersId: null,
        customersId: null,
        contecterId: null,
        ordersTime: null,
        sendersName: null,
        sendersPhone: null,
        sendersAddress: null,
        sendersAddressDetail: null,
        receiversName: null,
        receiversPhone: null,
        receiversAddress: null,
        receiversAddressDetail: null
      };
      this.packagesList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordersId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ordersId = row.ordersId || this.ids
      getOrders(ordersId).then(response => {
        this.form = response.data;
        this.packagesList = response.data.packagesList;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.packagesList = this.packagesList;
          if (this.form.ordersId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordersIds = row.ordersId || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ordersIds + '"的数据项？').then(function() {
        return delOrders(ordersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 货物管理序号 */
    rowPackagesIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 货物管理添加按钮操作 */
    handleAddPackages() {
      let obj = {};
      obj.packagesName = "";
      obj.packingtype = "";
      obj.packingname = "";
      obj.unitName = "";
      obj.length = "";
      obj.width = "";
      obj.height = "";
      obj.weight = "";
      obj.temperature = "";
      obj.shelfLife = "";
      this.packagesList.push(obj);
    },
    /** 货物管理删除按钮操作 */
    handleDeletePackages() {
      if (this.checkedPackages.length == 0) {
        this.$modal.msgError("请先选择要删除的货物管理数据");
      } else {
        const packagesList = this.packagesList;
        const checkedPackages = this.checkedPackages;
        this.packagesList = packagesList.filter(function(item) {
          return checkedPackages.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePackagesSelectionChange(selection) {
      this.checkedPackages = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo2/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
